<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class Privilege extends Admincontroller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $res = Db::query('select p.id pid,r.id rid, r.name rname,p.controller, p.method from xo_admin_role_pp rp, xo_admin_pp p,xo_admin_role r where r.id=rp.rid and p.id=rp.pid  order by r.id asc');
        $data = Db::table('xo_admin_pp')->field(true)->select();
        foreach ($res as $k=>$v) {
            if ($v['rname'] == 'root') {
                unset($res[$k]);
            }
        }
        // var_dump($res);die;
        return view('index', ['list' => $res, 'data' => $data]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //得到了 rid 1  role角色 root
        //        选框值 123
        $select = input('post.');
        var_dump($select);die;
        $rid = $select['rid'];
        $role = $select['role'];
        unset($select['rid']);
        unset($select['role']);
        foreach ($select as $k => $v) {
            foreach ($v as $a => $b) {
                $xuan[] = $v[$a];
            }
        }
        //$xuan是用户提交的权限  $list是角色本来有的选线

        //先取出这个角色现有的权限ID
        $res = DB::table('xo_admin_role_pp')->field('pid')->where(['rid' => $rid])->select();
        foreach ($res as $key => $v) {
            $list[] = $v['pid'];
        }
        $acc = array_intersect($xuan, $list);
        //如果去重后$acc为空 说明本来没有这个权限 可以直接添加 $xuan
        //如果去重后$acc 不为空(有数字) 说明本来有这个权限 从传入的$xuan中删除该值 在判断$xuan是否为空 如果为空说明选的都是已有的 提示  如果不为空 那么新增
        if (empty($acc)) {
            foreach ($xuan as $aa => $bb) {
                $data = ['rid' => $rid, 'pid' => $xuan[$aa]];
                DB::table('xo_admin_role_pp')->insert($data);
            }
            return $this->success('新增权限成功', url('admin/privilege/index'));
        } else {
            foreach ($acc as $aa => $bb) {
                unset($xuan[$aa]);
            }
            if (empty($xuan)) {
                return $this->error('该角色已有该权限,请重试', url('admin/privilege/index'));
            } else {
                dump($xuan);
                foreach ($xuan as $aa => $bb) {
                    $data = ['rid' => $rid, 'pid' => $xuan[$aa]];
                    DB::table('xo_admin_role_pp')->insert($data);
                }
                return $this->success('新增权限成功', url('admin/privilege/index'));
            }
        }
    }


    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $pid = input('delete.');
        if ($pid == '7') {
            $info['status'] = false;
        } else {
            $res = DB::table('xo_admin_role_pp')->where(['rid' => $id, 'pid' => $pid['pid']])->delete();
            if ($res > 0) {
                $info['status'] = true;
            } else {
                $info['status'] = false;
            }
        }
        return json($info);
    }

    public function addrole()
    {
        $role=input('post.');
        $userid=Db::table('xo_admin_role')->insertGetId($role);
        if($userid>0){
            $map['rid']=$userid;
            $map['pid']=7;
            $num=DB::table('xo_admin_role_pp')->insert($map);
            if($num>0){
                return $this->success('新增成功',url('admin/privilege/index'));
            }else{
                return $this->error('新增角色失败，请稍后再试',url('admin/privilege/index'));
            }
        }else{
            return $this->error('新增角色失败，请稍后再试',url('admin/privilege/index'));
        }
    }

}
